Switch ================= 根据条件选择输入张量。该算子根据布尔条件 `condition` 的值,选择 `input_x` 或 `input_y` 作为输出。该算子不区分数据类型,适用于所有数据类型。 .. math:: \text{output} = \begin{cases} \text{input\_x}, & \text{if } \text{condition} = \text{True} \\ \text{input\_y}, & \text{if } \text{condition} = \text{False} \end{cases} 该算子不复制数据,只是将输出指针指向选中的输入张量。因此,输出张量共享输入张量的数据指针和元数据。 输入: - **input_x** - 第一个输入张量(TensorC* 类型)。当 `condition` 为 True 时被选中。 - **input_y** - 第二个输入张量(TensorC* 类型)。当 `condition` 为 False 时被选中。 - **condition** - 条件值(bool 类型),决定选择哪个输入张量。 输出: - **output** - 输出张量指针的指针(TensorC** 类型),指向选中的输入张量。 支持平台: ``FT78NE`` ``MT7004`` .. note:: - 该算子不区分数据类型,适用于所有数据类型 - 算子不复制数据,输出张量共享输入张量的数据指针 - 输出张量的所有元数据(形状、数据类型、格式等)与选中的输入张量相同 **共享存储版本:** .. c:function:: void switch_s(TensorC* input_x, TensorC* input_y, TensorC** output, bool condition) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 32 //FT78NE示例 #include #include int main(int argc, char* argv[]) { TensorC input_x; TensorC input_y; TensorC* output; // 初始化 input_x int x_shape[3] = {2, 3, 4}; memcpy(input_x.shape_, x_shape, 3 * sizeof(int)); input_x.shape_size_ = 3; input_x.data_type_ = kNumberTypeFloat32; input_x.format_ = Format_NCHW; input_x.data_ = (void *)0xA0000000; input_x.category_ = 0; // 非常量 input_x.shape_changed_ = false; // 初始化 input_y int y_shape[3] = {2, 3, 4}; memcpy(input_y.shape_, y_shape, 3 * sizeof(int)); input_y.shape_size_ = 3; input_y.data_type_ = kNumberTypeFloat32; input_y.format_ = Format_NCHW; input_y.data_ = (void *)0xB0000000; input_y.category_ = 0; input_y.shape_changed_ = false; bool condition = true; // 选择 input_x switch_s(&input_x, &input_y, &output, condition); return 0; } **私有存储版本:** .. c:function:: void switch_p(TensorC* input_x, TensorC* input_y, TensorC** output, bool condition) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 32 //FT78NE示例 #include #include int main(int argc, char* argv[]) { TensorC input_x; TensorC input_y; TensorC* output; // 初始化 input_x int x_shape[3] = {2, 3, 4}; memcpy(input_x.shape_, x_shape, 3 * sizeof(int)); input_x.shape_size_ = 3; input_x.data_type_ = kNumberTypeFloat32; input_x.format_ = Format_NCHW; input_x.data_ = (void *)0x10000000; input_x.category_ = 0; // 非常量 input_x.shape_changed_ = false; // 初始化 input_y int y_shape[3] = {2, 3, 4}; memcpy(input_y.shape_, y_shape, 3 * sizeof(int)); input_y.shape_size_ = 3; input_y.data_type_ = kNumberTypeFloat32; input_y.format_ = Format_NCHW; input_y.data_ = (void *)0x10001000; input_y.category_ = 0; input_y.shape_changed_ = false; bool condition = true; // 选择 input_x switch_p(&input_x, &input_y, &output, condition); return 0; }